package com.qyhl.module_practice.newhome.scan;

import android.graphics.Bitmap;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ToggleButton;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.gyf.immersionbar.ImmersionBar;
import com.qyhl.module_practice.R;
import com.qyhl.module_practice.R2;
import com.qyhl.webtv.basiclib.base.BaseActivity;
import com.qyhl.webtv.basiclib.base.BaseIViewPresenter;
import com.qyhl.webtv.basiclib.utils.dialog.OtherDialog;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.utils.action.ActionConstant;
import com.qyhl.webtv.commonlib.utils.action.ActionLogUtils;
import com.uuzuche.lib_zxing.activity.CaptureFragment;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = ARouterPathConstant.PRACTICE_SCAN)
public class PracticeScanActivity extends BaseActivity implements PracticeScanContract.PracticeScanView {
    @BindView(R2.id.light_btn)
    ToggleButton lightBtn;

    private PracticeScanPresenter mPresenter;
    private String code;
    private String username;


    @Override
    protected int getContentView() {
        return R.layout.practice_activity_scan;
    }

    @Override
    protected void setImmersionBar(ImmersionBar mImmersionBar) {
        mImmersionBar.fullScreen(true).init();
    }

    @Override
    protected BaseIViewPresenter initBaseIViewPresenter() {
        return null;
    }

    @Override
    protected void init() {
        mPresenter = new PracticeScanPresenter(this);
        CaptureFragment captureFragment = new CaptureFragment();
        // 为二维码扫描界面设置定制化界面
        CodeUtils.setFragmentArgs(captureFragment, R.layout.practice_scan_camera);
        captureFragment.setAnalyzeCallback(analyzeCallback);
        getSupportFragmentManager().beginTransaction().replace(R.id.fl_my_container, captureFragment).commit();
    }

    @Override
    protected void setListener() {
        lightBtn.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                CodeUtils.isLightEnable(true);
            } else {
                CodeUtils.isLightEnable(false);
            }
        });
    }

    /**
     * 二维码解析回调函数
     */
    CodeUtils.AnalyzeCallback analyzeCallback = new CodeUtils.AnalyzeCallback() {
        @Override
        public void onAnalyzeSuccess(Bitmap mBitmap, String result) {
            showLoading();
            username = CommonUtils.getInstance().getUserName();
            code = result;
            mPresenter.doSign(username, code, "signIn");
        }

        @Override
        public void onAnalyzeFailed() {
            showToast("解析二维码失败！");
        }
    };

    @OnClick(R2.id.cancel_btn)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void signSuccess(String msg) {
        dismissLoading();
        showToast(msg);
        finish();
    }

    @Override
    public void signError(String msg) {
        dismissLoading();
        showToast(msg);
    }

    @Override
    public void isSignOut() {
        new OtherDialog.Builder(PracticeScanActivity.this)
                .setContentView(R.layout.practice_dialog_edu_course_teacher)
                .setCancelable(false)
                .setCanceledOnTouchOutside(false)
                .setText(R.id.title, "已签到，是否签退？如果提前签退将会影响您的服务时长！")
                .setOnClickListener(R.id.negative_btn, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                })
                .setOnClickListener(R.id.positive_btn, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mPresenter.doSign(username, code, "signOut");
                    }
                }).show();
    }

    @Override
    protected void onResume() {
        super.onResume();
        ActionLogUtils.getInstance().onResume(this, ActionConstant.PRACTICE_SCAN);
    }

    @Override
    protected void onPause() {
        super.onPause();
        ActionLogUtils.getInstance().onPause(this, ActionConstant.PRACTICE_SCAN);
    }


}
